<?php

namespace app\api\controller;

use app\api\model\Third;
use app\api\model\User as UserModel;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['Login','curl_get'];
    protected $noNeedRight = '*';


    public function Login(){
        $param = request()->param();
        $code=$param['code'];
        $appid = Config::get('site.appid');//小程序的appid
        $appSecret = Config::get('site.secret');// 小程序的$appSecret
        $wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
        $getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid，appsecret，code拼接到url里
        $result = self::curl_get($getUrl);//请求拼接好的url
        $wxResult = json_decode($result, true);
        if (empty($wxResult)) {
            $this->error('获取openid时异常，微信内部错误');
        }else{
            if(!empty($wxResult['errcode'])){
                $this->error($wxResult['errmsg']);
            }
            $openid = $wxResult['openid'];
            $where_third['openid']=$openid;
            $Third = new Third();
            $User = new UserModel();
            $Third_find= $Third->get($where_third);
            if(empty($Third_find)){
                $User->nickname=$this->config_data('nickname')."_".rand(1111,9999);
                $User->avatar=$this->config_data('avatar');
                $User->jointime=time();
                $User->status="normal";
                $User->save();
                $Third->user_id=$User->id;
                $Third->platform="微信";
                $Third->apptype="小程序";
                $Third->openid=$openid;
                $Third->save();
                $data['is_mp']=0;
                $this->auth->direct($User->id);
            }else{
                $this->auth->direct($Third_find['user_id']);
                $user_find=$User->get($Third_find['user_id']);
                if(empty($user_find['mp_openid'])){
                    $is_mp=0;
                }else{
                    $is_mp=1;
                }
                $data['is_mp']=$is_mp;
            }
            $data['sessionKey']=$wxResult['session_key'];
            $data['token']=$this->auth->getToken();
            $data['openid']=$openid;
            $data['user_id']=$this->auth->id;
            $this->success('成功',$data);
        }
    }

    public function getopenid(){
        $param=$this->request->param();
        $appid=Config::get('site.mp_appid');
        $secret=Config::get('site.mp_secret');
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$param['code']."&grant_type=authorization_code";
        $rr=$this->curl_get($url);
        $datase=json_decode($rr, true);
        $user=$this->auth;
        db('user')->where(['id'=>$param['user_id']])->update(['mp_openid'=>$datase['openid']]);
        $this->success('请求成功',$datase);
    }

    public function info()
    {
        $user=$this->auth;
        $data['nickname']=$user->nickname;
        $data['avatar']=$user->avatar;
        $data['num']=$user->num;
        $this->success('',$data);
    }
    public function edit()
    {
        $param=$this->request->param();
        $user=$this->auth;
        Db::name('user')->where(['id'=>$user->id])->update($param);
        $this->success('');
    }
    public function my_building()
    {
        $list= Db::name('user_building')->where(['user_id'=>$this->auth->id])->select();
        $this->success('',$list);
    }
    public function building()
    {
        $param=$this->request->param();
        $user=$this->auth;
        $find=Db::name('building')->where(['num'=>$param['num']])->find();
        if(empty($find)){
            $this->error('没有此户号');
        }
        $find_user_building=Db::name('user_building')->where(['user_id'=>$user->id,'num'=>$param['num']])->find();
        if(!empty($find_user_building)){
            $this->error('此户号已绑定或审核中');
        }
        $param['user_id']=$user->id;
        Db::name('user_building')->insert($param);
        $this->success('');
    }
    public function bill()
    {
        $param=$this->request->param();
        $list=Db::name('waterrate')->where(['num'=>$param['num']])->order('id desc')->paginate(10);
        $this->success('',$list);
    }



    public function curl_get($url, &$httpCode = 0) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }

}
